package top.abilities.util.util.httpClient;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;
import java.io.*;
import org.slf4j.Logger;


public class SimpleHttpUtils {
	private static Logger logger = LoggerFactory.getLogger(SimpleHttpUtils.class);
	/**
	 * 发送json格式的请求
	 * @param url
	 * @param json
	 * @return
	 * @throws IOException
	 */
  public static String doPostJson(String url, String json) throws IOException {  
	        // 创建Httpclient对象  
	        CloseableHttpClient httpClient = HttpClients.createDefault();
	        CloseableHttpResponse response = null;
	        String resultString = "";  
	        try {  
	            // 创建Http Post请求  
	            HttpPost httpPost = new HttpPost(url);
	            // 创建请求内容  
	            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
	            httpPost.setEntity(entity);  
	            // 执行http请求  
	            response = httpClient.execute(httpPost);  
	            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                response.close();  
	            } catch (IOException e) {  
	                // TODO Auto-generated catch block  
	                e.printStackTrace();  
	                throw e;
	            }  
	        }  
	  
	        return resultString;  
	    }  


	public static void main(String[] args) {}


}